package org.jnarcissus.matchers;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsEqual;
import org.jbrisk.text.StringUtils;
import org.jnarcissus.MonitoredInstanceMethodCall;
import org.jnarcissus.MonitoredInstanceMethodMatcher;

public class CallCountMatcher extends MonitoredInstanceMethodMatcher {

	private Matcher<Integer> matcher;
	private int actualCallCount;
	
	public CallCountMatcher(int expectedCallCount) {
	
		this.matcher = new IsEqual<Integer>(expectedCallCount);
	}
	
	public CallCountMatcher(Matcher<Integer> matcher) {
		
		this.matcher = matcher;
	}
	
	public void describeTo(Description description) {

		description.appendText("Expected call count: ");
		matcher.describeTo(description);
		description.appendText(StringUtils.NEW_LINE);
		description.appendText("Actual call count: <");
		description.appendText(Integer.toString(this.actualCallCount));
		description.appendText(">");
	}

	@Override
	public boolean matchesSafely(MonitoredInstanceMethodCall method) {
		
		this.actualCallCount = method.getCallCount();
		
		return matcher.matches(actualCallCount);
	}

}
